home *** CD-ROM | disk | FTP | other *** search
/ Aminet 3 / Aminet 3 - July 1994.iso / Aminet / util / misc / MUser17src.lha / MultiUser / src / Support / MList.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-07  |  6.4 KB  |  256 lines

  1. /************************************************************
  2. * MultiUser - MultiUser Task/File Support System                *
  3. * ---------------------------------------------------------    *
  4. * List Clone                                                                *
  5. * ---------------------------------------------------------    *
  6. * © Copyright 1993-1994 Geert Uytterhoeven                        *
  7. * All Rights Reserved.                                                    *
  8. ************************************************************/
  9.  
  10.  
  11. #include <exec/types.h>
  12. #include <exec/memory.h>
  13. #include <dos/dos.h>
  14. #include <dos/dosasl.h>
  15. #include <proto/exec.h>
  16. #include <proto/dos.h>
  17. #include <string.h>
  18. #include <libraries/multiuser.h>
  19. #include <proto/multiuser.h>
  20.  
  21. #include "MList_rev.h"
  22.  
  23.  
  24. char __VersTag__[] = VERSTAG;
  25.  
  26.  
  27. static void __regargs DumpInfo(struct FileInfoBlock *fib,
  28.                                          struct muUserInfo *uinfo,
  29.                                          struct muGroupInfo *ginfo, BOOL groups,
  30.                                          struct ExecBase *SysBase,
  31.                                          struct DosLibrary *DOSBase,
  32.                                          struct muBase *muBase);
  33.  
  34.  
  35.     /*
  36.      *        This table makes it easier to set the Protection Flags the right way
  37.      */
  38.  
  39. struct ProtectionEntry {
  40.     ULONG Bit;
  41.     ULONG ActiveLow;        /* NULL, or the same as Bit above */
  42. };
  43.  
  44.  
  45. int __saveds Start(char *arg)
  46. {
  47.     struct ExecBase *SysBase;
  48.     struct DosLibrary *DOSBase;
  49.     struct muBase *muBase = NULL;
  50.     struct RDArgs *rdargs;
  51.     struct muUserInfo *uinfo;
  52.     struct muGroupInfo *ginfo;
  53.     LONG argarray[] = {
  54. #define argDIR        0
  55. #define argGROUPS    1
  56.         NULL, NULL
  57.     };
  58.     struct AnchorPath *anchor;
  59.     ULONG files, dirs, blocks;
  60.     ULONG tfiles = 0, tdirs = 0, tblocks = 0;
  61.     LONG args[3];
  62.     char **name;
  63.     static char *defaultname[] = {
  64.         "", NULL
  65.     };
  66.     int numdirs = 0;
  67.     LONG error;
  68.     int rc = RETURN_OK;
  69.  
  70.     SysBase = *(struct ExecBase **)4;
  71.  
  72.     if ((!(DOSBase = (struct DosLibrary *)OpenLibrary("dos.library", 37))) ||
  73.          (!(muBase = (struct muBase *)OpenLibrary("multiuser.library", 39)))) {
  74.         rc = ERROR_INVALID_RESIDENT_LIBRARY;
  75.         goto Exit;
  76.     }
  77.  
  78.     rdargs = ReadArgs("DIR/M,GROUPS/S", argarray, NULL);
  79.     if (!rdargs)
  80.         error = IoErr();
  81.     else
  82.         if (uinfo = muAllocUserInfo()) {
  83.             if (ginfo = muAllocGroupInfo()) {
  84.                 if (anchor = (struct AnchorPath *)AllocVec(sizeof(struct AnchorPath),
  85.                                                                          MEMF_CLEAR)) {
  86.                     anchor->ap_BreakBits = SIGBREAKF_CTRL_C;
  87.                     anchor->ap_Flags = APF_DOWILD;
  88.                     if (argarray[argDIR])
  89.                         name = (char **)argarray[argDIR];
  90.                     else
  91.                         name = defaultname;
  92.                     do {
  93.                         files = dirs = blocks = 0;
  94.                         if (!(error = MatchFirst(name[0], anchor))) {
  95.                             if (IsFileSystem(name[0]))
  96.                                 if (!(anchor->ap_Flags & APF_ITSWILD) &&
  97.                                      (anchor->ap_Info.fib_DirEntryType > 0)) {
  98.                                     if (strlen(name[0])) {
  99.                                         args[0] = (LONG)name[0];
  100.                                         VPrintf("Directory \"%s\"\n", args);
  101.                                     }
  102.                                     anchor->ap_Flags |= APF_DODIR;
  103.                                     error = MatchNext(anchor);
  104.                                 } else {}
  105.                             else                    
  106.                                 error = ERROR_OBJECT_WRONG_TYPE;
  107.                             if (!error)
  108.                                 do
  109.                                     if (anchor->ap_Flags & APF_DIDDIR)
  110.                                         anchor->ap_Flags &= ~APF_DIDDIR;
  111.                                     else {
  112.                                         DumpInfo(&anchor->ap_Info, uinfo, ginfo,
  113.                                                     argarray[argGROUPS], SysBase, DOSBase,
  114.                                                     muBase);
  115.                                         if (anchor->ap_Info.fib_DirEntryType < 0)
  116.                                             files++;
  117.                                         else
  118.                                             dirs++;
  119.                                         blocks += anchor->ap_Info.fib_NumBlocks+1;
  120.                                     }
  121.                                 while (!(error = MatchNext(anchor)));
  122.                             if (error == ERROR_NO_MORE_ENTRIES)
  123.                                 error = NULL;
  124.                         } else if (error == ERROR_NO_MORE_ENTRIES)
  125.                             error = ERROR_OBJECT_NOT_FOUND;
  126.                         if (!error) {
  127.                             args[0] = files;
  128.                             args[1] = dirs;
  129.                             args[2] = blocks;
  130.                             VPrintf("%ld files - %ld directories - %ld blocks used\n\n",
  131.                                       args);
  132.                             tfiles += files;
  133.                             tdirs += dirs;
  134.                             tblocks += blocks;
  135.                             numdirs++;
  136.                         }
  137.                         MatchEnd(anchor);
  138.                     } while (!error && *(name = &name[1]));
  139.                     if (!error && (numdirs > 1)) {
  140.                         PutStr("TOTAL: ");
  141.                         args[0] = tfiles;
  142.                         args[1] = tdirs;
  143.                         args[2] = tblocks;
  144.                         VPrintf("%ld files - %ld directories - %ld blocks used\n",
  145.                                   args);
  146.                     }
  147.                     FreeVec(anchor);
  148.                 } else
  149.                     error = IoErr();
  150.                 muFreeGroupInfo(ginfo);
  151.             } else
  152.                 error = IoErr();
  153.             muFreeUserInfo(uinfo);
  154.         } else
  155.             error = IoErr();
  156.     FreeArgs(rdargs);
  157.     if (error) {
  158.         PrintFault(error, NULL);
  159.         rc = RETURN_ERROR;
  160.     }
  161.  
  162. Exit:
  163.     CloseLibrary((struct Library *)muBase);
  164.     CloseLibrary((struct Library *)DOSBase);
  165.  
  166.     return(rc);
  167. }    
  168.  
  169.  
  170.     /*
  171.      *        Dump the Information for a Directory Entry
  172.      */
  173.  
  174. static void __regargs DumpInfo(struct FileInfoBlock *fib,
  175.                                          struct muUserInfo *uinfo,
  176.                                          struct muGroupInfo *ginfo, BOOL groups,
  177.                                          struct ExecBase *SysBase,
  178.                                          struct DosLibrary *DOSBase,
  179.                                          struct muBase *muBase)
  180. {
  181.     ULONG stream[6];
  182.     char protection[17];
  183.     char *fmt;
  184.     int i = 0;
  185.     struct DateTime dt;
  186.     char date[LEN_DATSTRING];
  187.  
  188.     static struct ProtectionEntry pe[] = {
  189.         muFIBF_SET_UID, FALSE,
  190.         FIBF_SCRIPT, FALSE,
  191.         FIBF_PURE, FALSE,
  192.         FIBF_ARCHIVE, FALSE,
  193.         FIBF_READ, FIBF_READ,
  194.         FIBF_WRITE, FIBF_WRITE,
  195.         FIBF_EXECUTE, FIBF_EXECUTE,
  196.         FIBF_DELETE, FIBF_DELETE,
  197.         FIBF_GRP_READ, FALSE,
  198.         FIBF_GRP_WRITE, FALSE,
  199.         FIBF_GRP_EXECUTE, FALSE,
  200.         FIBF_GRP_DELETE, FALSE,
  201.         FIBF_OTR_READ, FALSE,
  202.         FIBF_OTR_WRITE, FALSE,
  203.         FIBF_OTR_EXECUTE, FALSE,
  204.         FIBF_OTR_DELETE, FALSE,
  205. };
  206.     static char ProtectionTemplate[] = "usparwedrwedrwed";
  207.  
  208.     stream[i++] = (ULONG)&fib->fib_FileName;
  209.     if (fib->fib_DirEntryType < 0)
  210.         stream[i++] = fib->fib_Size;
  211.     dt.dat_Stamp = fib->fib_Date;
  212.     dt.dat_Format = FORMAT_DOS;
  213.     dt.dat_Flags = NULL;
  214.     dt.dat_StrDay = NULL;
  215.     dt.dat_StrDate = date;
  216.     dt.dat_StrTime = NULL;
  217.     DateToStr(&dt);
  218.     stream[i++] = (ULONG)protection;
  219.     stream[i++] = (ULONG)date;
  220.     if (fib->fib_OwnerUID == muNOBODY_UID) {
  221.         strcpy(uinfo->UserID, "\b");
  222.         groups = FALSE;
  223.     } else {
  224.         uinfo->uid = fib->fib_OwnerUID;
  225.         if (!muGetUserInfo(uinfo, muKeyType_uid))
  226.             strcpy(uinfo->UserID, "???");
  227.     }
  228.     stream[i++] = (LONG)&uinfo->UserID;
  229.  
  230.     if (groups) {
  231.         ginfo->gid = fib->fib_OwnerGID;
  232.         if (!muGetGroupInfo(ginfo, muKeyType_gid))
  233.             strcpy(ginfo->GroupID, "???");
  234.         stream[i] = (LONG)&ginfo->GroupID;
  235.     }
  236.  
  237.     for (i = 0; i < 16; i++)
  238.         if ((fib->fib_Protection & pe[i].Bit) ^ pe[i].ActiveLow)
  239.             protection[i] = ProtectionTemplate[i];
  240.         else
  241.             protection[i] = '-';
  242.     protection[i] = '\0';
  243.  
  244.     if (fib->fib_DirEntryType < 0)
  245.         if (groups)
  246.             fmt = "%-30s %8ld %s %s %s [%s]\n";
  247.         else
  248.             fmt = "%-30s %8ld %s %s %s\n";
  249.     else
  250.         if (groups)
  251.             fmt = "%-30s      DIR %s %s %s [%s]\n";
  252.         else
  253.             fmt = "%-30s      DIR %s %s %s\n";
  254.     VPrintf(fmt, stream);
  255. }
  256.